//
//  WBTabBar.m
//  微博
//
//  Created by 陈帅府 on 15-3-21.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "WBTabBar.h"
@interface  WBTabBar()
@property(nonatomic,weak)UIButton * plusBut;
@end
@implementation WBTabBar

//通过纯代码创建时调用
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
//通过xib/storyboard创建时调用
- (id)initWithCoder:(NSCoder *)coder{

    self = [super initWithCoder:coder];
    if (self) {
        [self setUp];
    }
    return self;
}
- (void)setUp
{
//创建一个按钮
    UIButton *plusBut = [[UIButton alloc]init];
    plusBut.tag = 111;
//设置button 的背景图片
    //normal状态
    [plusBut setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
//高亮状态
    [plusBut setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
//设置按钮的图片
    [plusBut setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_add"] forState:UIControlStateNormal];
//高亮状态
    [plusBut setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    //给plusbut赋值
    
    self.plusBut = plusBut;
    //layoutSubviews方法中设置frame
    
    //将按钮添加到tabbar中去
    
    [self addSubview:plusBut];
    
}

//自定义tabBar之后修改子控制器的frame
- (void)layoutSubviews{
    [super layoutSubviews];
    //先遍历一下tabBar.subviews的子控制器的个数,确定自己所想要的数据
    NSInteger  index = 0;
    for (UIView *child in self.subviews) {
    //        if ([child isKindOfClass:[UIControl class]]&&![child isKindOfClass:[UIButton class]]) {
//        if ([child isKindOfClass:[UIControl class]]&&child .tag !=111) {
    if ([child isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            CGFloat childW =self.frame.size.width/5;
            CGFloat childH =self.frame.size.height;
            CGFloat childX =index *childW;
            CGFloat childY =0;
            if (index==2) {
                index++;
                childX =index *childW;
            }
            index++;
            child.frame = CGRectMake(childX, childY, childW, childH);
        }
        
    }
    //判断child的父类是谁
    /*
    for (UIView *child in self.subviews) {
        //判断child的类，由于UITabBarButton是私有的不能访问所以要查询child的父类
        DDLogDebug(@"%@",child);
        DDLogDebug(@"%@",child.superclass);
    }
*/
    
self.plusBut.size = self.plusBut.currentBackgroundImage.size;

    self.plusBut.centerX = self.width * 0.5;
    self.plusBut.centerY = self.height * 0.5;

    DDLogDebug(@"height %f,width %f, %f",self.plusBut.currentBackgroundImage.size.height ,self.plusBut.currentBackgroundImage.size.width,self.plusBut.centerY);
}



@end
